#include <math.h>
#include <stdlib.h>

const double pi = 3.1415926535;

extern "C" {

  	double Interpolate_Cosine(signed int a, signed int b, double pos) {
		pos = (1.0-(0.5+(cos((pos-(int)pos)*pi)*0.5)));
		return a+((b-a)*pos);
	}


	void CalculatePan(double Pan,double EnvelopePan, double* LPanCalc, double* RPanCalc) {
		double FinalPan=(Pan+(EnvelopePan-32.0)*(128.0-abs(Pan-128.0))/32.0)/128.0;
		if (FinalPan>0.0) {
			*LPanCalc = 1.0-FinalPan;
			*RPanCalc = FinalPan;
			return; 
		} else if (FinalPan<0.0) {
			*LPanCalc = abs(FinalPan);
			*RPanCalc = 1.0-abs(FinalPan);
			return;
		} else {
			*LPanCalc=1.0;
			*RPanCalc=1.0;
			return; 
		}
	}


	int Convert32BitLRTo16Bit(int* LBuf32, int* RBuf32, short* Buf16, int length, double vol) {
		double av;
		int pos;
		for (pos=0; pos<length; pos++) {
			av = (*LBuf32)*vol;
			if (av>=32767) {av=32767;} else {if (av<=-32767) {av=-32767;}}
			*Buf16=av+0.5; Buf16++; LBuf32++;
			av = (*RBuf32)*vol;
			if (av>=32767) {av=32767;} else {if (av<=-32767) {av=-32767;}}
			*Buf16=av+0.5; Buf16++; RBuf32++;
		}
	}

}